/******************************************************************
 *
 *    Java Lib For Android, Powered By Shenzhen Jiuzhou.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.etc.controller.role
 *
 *    Filename:    RoleController.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author:     Administrator
 *
 *    @version:    1.0.0
 *
 *    Create at:   2016年3月24日 下午10:16:12
 *
 *    Revision:
 *
 *    2016年3月24日 下午10:16:12
 *        - first revision
 *
 *****************************************************************/
package com.etc.controller.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.etc.common.enums.StatusEnum;
import com.etc.common.result.ResultMapper;
import com.etc.common.util.DateUtils;
import com.etc.common.util.StringUtil;
import com.etc.role.service.IRoleService;
import com.etc.role.vo.RoleVO;

/**
 * @ClassName RoleController
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author 张亮
 * @Date 2016年3月24日 下午10:16:12
 * @version 1.0.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    
    @RequestMapping(method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> list(@RequestParam(value="page",required=false)int page,@RequestParam(value="start",required=false)int start,@RequestParam(value="limit",required=false)int limit){
        Map<String, Object> param=new HashMap<String, Object>();
        param.put("pageNo",page);
        param.put("pageNum", limit);
        List<RoleVO> list=roleService.list(param);
        int count=roleService.count(param);
        return ResultMapper.convertSuccess(list,count);
    }
    
    @RequestMapping(method=RequestMethod.POST)
    public Map<String,Object> save(@RequestBody RoleVO roleVO){
        roleVO.setTsrStatus(StatusEnum.ENABLE.getIndex().toString());
        roleVO.setTsrUpdateTime(DateUtils.getCurrentDate());
        int result=roleService.insert(roleVO);
        if(result>0){
            return ResultMapper.convertSuccess();
        }else{
            return ResultMapper.convertFailure();
        }
    }
    @RequestMapping(value="/{tsrId}",method=RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> update(@PathVariable("tsrId")String tsrId,@RequestBody RoleVO roleVO){
        roleVO.setTsrUpdateTime(DateUtils.getCurrentDate());
        roleService.update(roleVO);
        return ResultMapper.convertSuccess();
    }
    

    @RequestMapping(value="/{tsrId}",method=RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("tsrId")String tsrId){
        if(StringUtil.isNotEmpty(tsrId)){
            roleService.delete(Integer.valueOf(tsrId));
            return ResultMapper.convertSuccess();
        }
        return ResultMapper.convertFailure();
    }
    
    @RequestMapping(value="/updatestatus",method=RequestMethod.POST)
    public Map<String,Object> save(HttpServletRequest request){
        String tsrId=request.getParameter("tsrId");
        String tsrStatus=request.getParameter("tsrStatus");
        if(StringUtil.isNotEmpty(tsrId)&&StringUtil.isNotEmpty(tsrStatus)){
            RoleVO roleVO=new RoleVO();
            roleVO.setTsrId(tsrId);
            roleVO.setTsrStatus(tsrStatus);
            roleVO.setTsrUpdateTime(DateUtils.getCurrentDate());
            roleService.update(roleVO);
            return ResultMapper.convertSuccess();
        }
        return ResultMapper.convertFailure();
        
    }
}
